package com.module.ble.db

import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.Update
import com.module.ble.db.table.UserStepNumber
import kotlinx.coroutines.flow.Flow

/**
 * 作者：sosou
 * <p>
 * 版本：1.0
 * <p>
 * 创建日期：2025/3/25
 * <p>
 * 描述：
 * <p>
 * 修订历史：
 */
@Dao
interface StepNumberDao {

    @Insert(onConflict = OnConflictStrategy.IGNORE)
    fun insert(vararg bean: UserStepNumber)

    @Insert(onConflict = OnConflictStrategy.IGNORE)
    fun insert(mList:List<UserStepNumber>)

    @Query("SELECT createdTime FROM step_number WHERE createdTime IN (:times)")
    fun getExistingTimes(times: List<Long>): List<Long>

    @Query("SELECT * FROM step_number")
    fun findAll(): MutableList<UserStepNumber>

    @Query("SELECT * FROM step_number")
    fun findAllFlow(): Flow<List<UserStepNumber>>

    // 先获取最新日期，再查询该日期的所有数据
    @Query("SELECT * FROM step_number WHERE date(createdTime/1000, 'unixepoch', 'localtime') = (SELECT date(MAX(createdTime)/1000, 'unixepoch', 'localtime') FROM step_number) ORDER BY createdTime DESC")
    fun findLatestDayRecords(): List<UserStepNumber>

    @Query("SELECT * FROM step_number WHERE date(createdTime/1000, 'unixepoch', 'localtime') = (SELECT date(MAX(createdTime)/1000, 'unixepoch', 'localtime') FROM step_number) ORDER BY createdTime DESC")
    fun findLatestDayFlow(): Flow<List<UserStepNumber>>

    // 查询 所有  倒序 查询
    @Query("SELECT * FROM step_number ORDER BY createdTime DESC  LIMIT :mSize")
    fun findByDescSize(mSize: Int): MutableList<UserStepNumber>

    @Update
    fun update(msg: UserStepNumber)

    @Query("select count(*) from step_number")
    fun count(): Int

    //查询某条数据
    @Query("SELECT * FROM step_number WHERE createdTime = :time")
    fun findMessageByTime(time:Long): UserStepNumber

    @Delete
    fun delete(data: UserStepNumber)

    //删除所有 → '所有'的只能用@Query,@Query可以编写任意的sql语句
    @Query("DELETE FROM step_number")
    fun deleteAll()

    @Query("DELETE FROM step_number WHERE createdTime = :time")
    fun deleteMsgByTime(time:Long)

}